package com.lxj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lxj.common.CodeMsg;
import com.lxj.common.Result;
import com.lxj.domain.SysUser;
import com.lxj.dto.SysUserDto;
import com.lxj.mapper.SysUserMapper;
import com.lxj.service.UserService;
import com.lxj.util.MD5Utils;
import com.lxj.vo.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lxj
 * @date 2021/6/16 18:33
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 使用用户名查询用户
     * @param username 用户的名称
     * @return 数据库里面的用户对象
     */
    @Override
    public SysUser findUserByUsername(String username) {
        return sysUserMapper.selectUserByName(username);
    }

    /**
     * 新增用户
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result addUser(SysUserDto dto) {
        //验证dto里面的参数是否合法
        SysUser sysUserName = sysUserMapper.selectUserByName(dto.getLoginName());
        if (sysUserName != null) {
            return new Result(CodeMsg.USER_LOGIN_NAME_EXIST_ERROR);
        }
        SysUser sysUserIdCard = sysUserMapper.selectUserByIdCard(dto.getIdCard());
        if (sysUserIdCard != null) {
            return new Result(CodeMsg.USER_ID_CARD_EXIST_ERROR);
        }
        SysUser sysUserPhone = sysUserMapper.selectUserByPhone(dto.getPhone());
        if (sysUserPhone != null) {
            return new Result(CodeMsg.USER_PHONE_EXIST_ERROR);
        }
        SysUser sysUser = new SysUser();
        //将dto的属性值拷贝到domain里面
        BeanUtil.copyProperties(dto, sysUser);
        //处理domain 里面的默认字段
        sysUser.setCreateTime(new Date());
        sysUser.setDeleted(1);
        //加密用户的原始密码
        sysUser.setPassword(MD5Utils.encode(dto.getLoginPassword()));
        sysUserMapper.insert(sysUser);
        return new Result();
    }

    /**
     * 根据id删除用户
     * @param id
     * @return
     */
    @Override
    @Transactional
    public Result deleteUserById(Integer id) {
        sysUserMapper.deleteByPrimaryKey(id);
        return new Result();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public Result batchDelete(Integer[] ids) {
        sysUserMapper.batchDelete(ids);
        return new Result();
    }

    /**
     * 修改用户
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public Result updateUser(SysUserDto dto) {
        SysUser sysUser = new SysUser();
        //将dto的属性值拷贝到domain里面
        BeanUtil.copyProperties(dto, sysUser);
        //处理domain 里面的默认字段
        sysUser.setDeleted(sysUser.getDeleted() == null ? 0 : sysUser.getDeleted());
        sysUser.setLoginName(null);
        sysUser.setPassword(null);
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
        return new Result();
    }

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageInfo<SysUser> selectPage(SysUserDto dto) {
        //开启分页插件
        Page<Object> page = PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<SysUser> users = sysUserMapper.selectUser(dto);
        return new PageInfo<SysUser>(page.getTotal(), users);
    }

}
